#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
int resx,resy,resz,cx,cy,color=15;
float zonx,yonx;

#define RES 0.1

void graph(float x,float y,float z)
{
	float px,py,pz;
	px=cx+x*resx;
	py=cy-y*resy;
	pz=z*resz;
	//z-adjustment:
	px+=zonx * pz;
	py+=yonx * pz;
	if(px>=0 && px<640 && py>=0 && py<480)	{
		putpixel(px,py,color);
	}
}

#define drawgraph(t,down,up,incr,x,y,z) for(t=down;t<up;t+=incr) graph(x,y,z)

void draw_cube(int w,int b,int h)
{
	float t;

	drawgraph(t,-b,b,RES,w,h,t);
	drawgraph(t,-b,b,RES,w,-h,t);
	drawgraph(t,-b,b,RES,-w,h,t);
	drawgraph(t,-b,b,RES,-w,-h,t);

	drawgraph(t,-w,w,RES,t,h,b);
	drawgraph(t,-w,w,RES,t,h,-b);
	drawgraph(t,-w,w,RES,t,-h,b);
	drawgraph(t,-w,w,RES,t,-h,-b);

	drawgraph(t,-h,h,RES,w,t,b);
	drawgraph(t,-h,h,RES,w,t,-b);
	drawgraph(t,-h,h,RES,-w,t,b);
	drawgraph(t,-h,h,RES,-w,t,-b);
}


void main()
{
	int gdriver=DETECT,gmode;
	float t,x,y,px,py,pz,z=0;
	resx=resy=10;
	resz=10;
	cx=320;
	cy=240;
	initgraph(&gdriver,&gmode,"");
	setcolor(15);
	outtextxy(0,0,"3D Graph");

	getch();

	zonx = -0.5;
	yonx = 0.5;

	for(zonx = -1; zonx < 1; zonx += 0.05) {

		color = 15;
		draw_cube(5, 5, 5);

		delay(100);

		color = 0;
		draw_cube(5, 5, 5);


	}

	color = 15;
	draw_cube(5,5,5);

	getch();
}